package com.liwy3.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Supplier;

/**
 * mybatis-plus 配置类
 **/

@Configuration
@MapperScan("com.liwy3.mapper")
public class MybatisPlusConfig implements StrategyMetaObjectHandler {

    /**
     * 处理分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 插入自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
 }

    /**
     * 更新自动填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {
    }

}

/**
 * 重写填充策略
 */
interface StrategyMetaObjectHandler extends MetaObjectHandler {

    /**
     * 不管有没有值,强制更新
     */
    @Override
    default MetaObjectHandler strictFillStrategy(MetaObject metaObject, String fieldName, Supplier<?> fieldVal) {
        metaObject.setValue(fieldName, fieldVal.get());
        return this;
    }
}
